package net.cgsoft.widget;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


/**
 * Created by Youga on 2015/12/25.
 */
public class TimeFragment extends DialogFragment {

    public static final String TAG = "TimeFragment";
    TimeFragmentCallBack callBack;
    TimePicker timePicker;
    Button btnCancel, btnConfirm;

    public static TimeFragment newInstance(TimeFragmentCallBack callBack) {
        TimeFragment fragment = new TimeFragment();
        fragment.setTimeFragmentCallBack(callBack);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Dialog dialog = getDialog();
        if (dialog != null) dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return inflater.inflate(R.layout.fragment_time, container, false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        timePicker = (TimePicker) view.findViewById(R.id.timePicker);
        btnCancel = (Button) view.findViewById(R.id.button_cancel);
        btnConfirm = (Button) view.findViewById(R.id.button_confirm);
        timePicker.setIs24HourView(true);

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                callBack.onTimeFocus(null);
            }
        });

        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                timePicker.clearFocus();
                int hour = timePicker.getCurrentHour();
                int minute = timePicker.getCurrentMinute();
                String time = (hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute);
                callBack.onTimeFocus(time);
            }
        });
    }

    public void setTimeFragmentCallBack(TimeFragmentCallBack callBack) {
        this.callBack = callBack;
    }

    public interface TimeFragmentCallBack {
        void onTimeFocus(String time);
    }
}
